<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索框和按钮 -->
      <el-row :gutter="20">
        <!-- 搜素框 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加按钮 -->
        <el-col :span="4">
          <!-- 跳转到Add.vue 的两种法法 -->
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
          <!-- <router-link to="/goods/add">
          <el-button type="primary">添加商品</el-button>
          </router-link> -->
        </el-col>
      </el-row>

      <!-- 主体表格区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat("YYYY-MM-DD hh:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.good_id)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        // 页码
        pagenum: 1,
        // 每页显示多少条
        pagesize: 6
      },
      // 总条数
      total: 0,
      // 存放数据的数组
      goodsList: []
    }
  },
  created () {
    this.getGoodList()
  },
  methods: {
    // 获取所有数据
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res)
      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    // 分页功能的函数
    // 监听pagesize
    handleSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getGoodList()
    },
    // 监听页码值的改变
    handleCurrentChange (num) {
      this.queryInfo.pagenum = num
      this.getGoodList()
    },
    // 点击删除按钮
    deleteGoods (id) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      })
        .then(async (action) => {
          await this.$http.delete('goods/' + id)
          this.getGoodList()
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="less" scoped>
</style>
